package Text1;

public class MyLinkedList implements SingleLinkedList {
    class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;// 代表当前链表的头节点的引用

    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    private void checkIndexadd(int index) throws ListIndexOutOfException {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfException("index位置不合法");
        }
    }

    private Node findIndexSubOne(int index) {
        Node cur = head;
        int count = index - 1;
        while (count <= 0) {
            cur = cur.next;
            count--;
        }
        return cur;
    }

    @Override
    public boolean addIndex(int index, int data) throws ListIndexOutOfException {
        checkIndexadd(index);
        if (index == 0) {
            addFirst(data);
            return true;
        }
        Node node = new Node(data);
        Node cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    private Node searchPrev(int key) {
        Node cur = head;
        while (cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    @Override
    public void remove(int key) {
        if (head == null) {
            return;
        }
        if (head.val == key) {
          head=head.next;
          return;
        }
        Node cur =searchPrev(key);
        if(cur==null){
            return;
        }
        cur.next=cur.next.next;
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        Node cur =head;
        while (cur.next!=null){
            if(cur.next.val==key){
                cur.next=cur.next.next;
            }else{
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        Node cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    @Override
    public void clear() {
        head = null;
    }


}
